package com.web.security.handler;

import com.web.security.share.DirectUrlResolver;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/5/3
 * Time: 14:01
 * To change this template use File | Settings | File Templates.
 */
public class MultipleAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    private List<DirectUrlResolver> resolvers = new ArrayList<>();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        for (DirectUrlResolver resolver : resolvers) {
            if (resolver.support(request)) {
                String directUrl = resolver.directUrl();
                setDefaultTargetUrl(directUrl);
            }
        }

        super.onAuthenticationSuccess(request, response, authentication);
    }

    public void setResolvers(List<DirectUrlResolver> resolvers) {
        this.resolvers = resolvers;
    }
}